from rest_framework.response import Response
from rest_framework import status
from rest_framework.viewsets import ModelViewSet
from django.http import StreamingHttpResponse
from django.utils.encoding import escape_uri_path

from equity_admin.serializers_activities import *
from equity_mall.utils.pages import MyPage
from equity_admin.tasks import *


class ChannelModelViewSet(ModelViewSet):

    serializer_class = ChannelModelSerializer
    filter_backends = (filters.SearchFilter, DjangoFilterBackend)
    search_fields = ('name', )
    filter_class = ChannelFilter

    def get_queryset(self):
        queryset = Channel.objects.all()
        return queryset

    def hand_movement(self, request):
        try:
            id = self.request.query_params.get('id', '1')
            url_dict = {
                '1': 'http://47.107.132.8:8000/alilogin1/',
                '2': 'http://47.107.132.8:8000/post_alilogin/',
                '3': 'http://47.107.132.8:8000/jx_alilogin/'
            }
            id_dict = {
                '1': 1,
                '2': 14,
                '3': 8,
            }
            update_ali_cookie.delay({'url': url_dict[id], 'id': id_dict[id]})
        except:
            pass
        return Response({'success': '成功'}, status=status.HTTP_200_OK)

    def ruiyinxin(self, request):
        try:
            url = self.request.data.get('url', None)
            body = self.request.data.get('body', None)
            response = requests.post(url=url, data=eval(body))
            return Response(response.json(), status=status.HTTP_200_OK)
        except:
            return Response({'error': '出错了'}, status=status.HTTP_200_OK)


class ActivityModelViewSet(ModelViewSet):

    pagination_class = MyPage
    filter_backends = (filters.SearchFilter, DjangoFilterBackend)
    search_fields = ('name', )
    serializer_class = ActivityListSerializer

    def get_queryset(self):
        user = self.request.iser
        level = user.level
        level_id = self.request.query_params.get('level', None)
        if level_id:
            try:
                level_list = get_level_list(level)
                level_id = int(level_id)
                if level_id in level_list:
                    level = Level.objects.filter(id=level_id).first()
            except:
                pass
        if level.level == 0:
            queryset = Activity.objects.all().order_by('level')
        elif level.level == 1:
            queryset = Activity.objects.filter(level__prov_id=level.id).order_by('level')
        elif level.level == 2:
            queryset = Activity.objects.filter(level__city_id=level.id).order_by('level')
        elif level.level == 3:
            queryset = Activity.objects.filter(level__district_id=level.id).order_by('level')
        else:
            queryset = Activity.objects.filter(level__branch_id=level.id).order_by('level')
        return queryset


class ActivityDataModelViewSet(ModelViewSet):
    '''派发'''

    pagination_class = MyPage
    filter_backends = (filters.SearchFilter, DjangoFilterBackend)
    # search_fields = ('username', 'mobile', 'nickname')
    filter_class = ActivityDataFilter

    def get_queryset(self):
        user = self.request.iser
        level = user.level
        level_id = self.request.query_params.get('level', None)
        if level_id:
            try:
                level_list = get_level_list(level)
                level_id = int(level_id)
                if level_id in level_list:
                    level = Level.objects.filter(id=level_id).first()
            except:
                pass
        if level.level == 0:
            queryset = ActivityData.objects.all().order_by('-create_time')
        elif level.level == 1:
            queryset = ActivityData.objects.filter(level__prov_id=level.id).order_by('-create_time')
        elif level.level == 2:
            queryset = ActivityData.objects.filter(level__city_id=level.id).order_by('-create_time')
        elif level.level == 3:
            queryset = ActivityData.objects.filter(level__district_id=level.id).order_by('-create_time')
        else:
            queryset = ActivityData.objects.filter(level__branch_id=level.id).order_by('-create_time')
        return queryset

    def get_serializer_class(self):
        if self.action == 'list':
            return ActivityDataListModelSerializer
        else:
            return ActivityDataRetrieveModelSerializer